package org.jeecg.modules.tls.csmg.csnotelike.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.modules.tls.csmg.csnote.entity.CsNote;
import org.jeecg.modules.tls.csmg.csnote.mapper.CsNoteMapper;
import org.jeecg.modules.tls.csmg.csnotelike.entity.CsNoteLike;
import org.jeecg.modules.tls.csmg.csnotelike.mapper.CsNoteLikeMapper;
import org.jeecg.modules.tls.csmg.csnotelike.service.ICsNoteLikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * @Description: cs_note_like
 * @Author: jeecg-boot
 * @Date:   2022-09-07
 * @Version: V1.0
 */
@Service
public class CsNoteLikeServiceImpl extends ServiceImpl<CsNoteLikeMapper, CsNoteLike> implements ICsNoteLikeService {

    @Autowired
    CsNoteLikeMapper csNoteLikeMapper;
    @Autowired
    CsNoteMapper csNoteMapper;
    @Override
    public Boolean setNoteLike(String userId, String noteId) {
        QueryWrapper<CsNoteLike> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId).eq("note_id",noteId);
        if(csNoteLikeMapper.exists(queryWrapper)){
            return false;
        }
        CsNote csNote = csNoteMapper.selectById(noteId);
        if(csNote.getHasPublic()<0){
            return false;
        }
        csNote.setHasPublic(csNote.getHasPublic()+1);
        csNoteMapper.updateById(csNote);
        CsNoteLike csNoteLike = new CsNoteLike();
        csNoteLike.setUserId(userId);
        csNoteLike.setNoteId(noteId);
        csNoteLikeMapper.insert(csNoteLike);
        return true;
    }

    @Override
    public Boolean cancelNoteLike(String userId, String noteId) {
        QueryWrapper<CsNoteLike> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId).eq("note_id",noteId);
        CsNote csNote = csNoteMapper.selectById(noteId);
        if(csNote.getHasPublic()<0){
            return false;
        }
        csNote.setHasPublic(csNote.getHasPublic()-1);
        if(csNote.getHasPublic()<0){
            return false;
        }
        if(csNoteLikeMapper.delete(queryWrapper)>0){
            csNoteMapper.updateById(csNote);
            return true;
        }else {
            return false;
        }


    }
}
